/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.messagedriven;

import javax.jms.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 *
 * @author Carlos
 */
@MessageDriven(mappedName = "jms/payMethodQueue", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})//*/
public class PayMethodsListener implements MessageListener{
    // Cola de los payMethod: payMethodQueue
    @Resource(mappedName = "jms/deliveryTopicFactory")
    private ConnectionFactory deliveryTopicFactory;

    @Override
    public void onMessage(Message message) {
//        if (message instanceof MapMessage) {
            MapMessage theMessage = (MapMessage) message;
            try {
                Long clientId = theMessage.getLong("clientId");
                System.out.println("Se pide info sobre el cliente : "+clientId);
                int ccnumber = theMessage.getInt("ccnumber");
                int secnumber = theMessage.getInt("secnumber");
                System.out.println("Sus datos bancarios son: ccnumber: "+ccnumber+" ; "+"secnumber: "+secnumber);
             
                Connection con = deliveryTopicFactory.createConnection();
                con.start();
                Session s = con.createSession(true, 0);
                MessageProducer producer = s.createProducer(message.getJMSReplyTo());
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
                TextMessage response = s.createTextMessage();
                response.setText("Hola");
                response.setJMSCorrelationID(message.getJMSCorrelationID());
                producer.send(response);
                producer.close();
                con.close();
            } catch (JMSException ex) {
                Logger.getLogger(DeliveryListener.class.getName()).log(Level.SEVERE, null, ex);
            } catch(Exception n){
                Logger.getLogger(DeliveryListener.class.getName()).log(Level.SEVERE, null, n);
            }
  //      }
    }
    
}
